/*
 * @LastEditTime: 2023-03-16 23:17:50
 * @Method: 无重复字符的最长子串
 * @Description: 利用set记录窗口元素,双指针控制窗口大小
 */
package CodeTop;

import java.util.HashSet;
import java.util.Set;

public class Solution3 {
    public int lengthOfLongestSubstring(String s) {
        if (s.equals("")) {
            return 0;
        }
        Set<Character> set = new HashSet<>();
        int l = 0, r = s.length() - 1;
        int maxLen = 1;
        for (int j = l;j <= r;j++) {
            char ch = s.charAt(j);
            if (set.contains(ch)) {
                while (s.charAt(l) != ch) {
                    set.remove(s.charAt(l));
                    l++;
                }
                set.remove(s.charAt(l));
                l++;
            }
            set.add(ch);
            maxLen = Math.max(maxLen, j - l + 1);
        }
        return maxLen;
    }

    public static void main(String[] args) {
        
    }
}
